<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>用户管理</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <!-- 引入样式 -->
<!--        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->

        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <!-- 引入组件库 -->
<!--        <script src="https://unpkg.com/element-ui/lib/index.js"></script>-->
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <style>
            .avatar-uploader .el-upload {
                border: 1px dashed #d9d9d9;
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
            }
            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }
            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }
            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统管理<small>菜单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="btFindPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
<!--                    懒加载表格-->
                    <el-table
                            :data="dataList"
                            style="width: 100%"
                            row-key="id"
                            border
                            lazy
                            :load="load"
                            :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                        <el-table-column prop="name" label="菜单名称" width="180"></el-table-column>
                        <el-table-column prop="linkUrl" label="访问路径" width="180"></el-table-column>
                        <el-table-column prop="path" label="路径级别"></el-table-column>
                        <el-table-column prop="parentMenuId" label="自关联ID"></el-table-column>
                        <el-table-column prop="level" label="菜单级别"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增/编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog  :title="dialogTitle" :visible.sync="dialogFormVisible">

                                    <el-form ref="formData" :model="formData" label-position="right" :rules="rules" label-width="100px">
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="菜单名称" prop="name">
                                                    <el-input v-model="formData.name"/>
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="访问路径" prop="linkUrl">
                                                    <el-input v-model="formData.linkUrl"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="路径级别" prop="path">
                                                    <el-input v-model="formData.path"/>
                                                </el-form-item>
                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item  label="优先级" prop="priority">
                                                    <el-input  v-model="formData.priority"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="自关联ID" prop="parentMenuId">
                                                    <el-select v-model="formData.parentMenuId" placeholder="请选择活动" :style="{width: '100%'}">
                                                        <el-option v-for="(item,index) in parentOptions" :key="item.id" :label="item.name"
                                                                   :value="item.id" ></el-option>
                                                    </el-select>
                                                </el-form-item>

                                            </el-col>
                                            <el-col :span="12">
                                                <el-form-item label="菜单级别" prop="level">
                                                    <el-input v-model="formData.level"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="图标" prop="icon">
                                                    <el-input v-model="formData.icon"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="注意事项">
                                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                    </el-form>
                            <div slot="footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handle(dialogTitle)">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>

    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                // 对话框标题
                dialogTitle:"",
                pagination: {
                    //分页相关属性
                    currentPage: 1,
                    pageSize:10,
                    total:100,
                    queryString:null,
                },

                dataList: [],//菜单列表数据
                tableData:[],//dialog窗口中权限列表数据
                parentOptions:[],//父菜单列表
                formData: {
                },//dialog表单数据
                dialogFormVisible: false,//控制添加窗口显示/隐藏

                rules: {
                    name:[{required: true, message: '请输入菜单名', trigger: 'blur'}],
                    linkUrl:[{required: true, message: '请输入访问路径', trigger: 'blur'}],
                    path:[{required: true, message: '请输入路径级别', trigger: 'blur'}],
                    priority:[{required: true, message: '请输入优先级', trigger: 'blur'}],
                    level:[{required: true, message: '请输入菜单级别', trigger: 'blur'}],
                    parentMenuId:[{required: true, message: '请选择父级菜单', trigger: 'change'}],
                },
            },

            created() {
                // 加载菜单信息
                this.findPage();
            },
            methods: {
                // 子菜单数据懒加载
                load(tree, treeNode, resolve) {
                    axios.get("/menu/childMenuById.do?parentId="+tree.id).then((res) => {
                        if (res.data.flag) {
                            //请求到了数据
                            setTimeout(() => {
                                resolve(
                                    res.data.data
                                )
                            }, 100)
                        } else {
                            this.$message.error(res.data.message)
                        }

                    })
                    // let data;
                    // if (tree.id===5) {
                    //     data=[{id:501,name:"五五五",address:"5的path"}
                    //     ,{id:502,name:"五五五",address:"5的path"}]
                    // }else if(tree.id===1){
                    //     data=[{id:401,name:"四四四",address:"4的path"}
                    //         ,{id:402,name:"四四四",address:"4的path"}]
                    // }


                }
                ,
                // dialog点击确定执行新增/修改方法
                handle(title) {
                    // 验证表单
                    this.$refs['formData'].validate(valid => {
                        if (!valid) return
                        let url = ""
                        if (title === "新增菜单信息") {
                            url = "add"
                        } else {
                            url = "edit"
                        }
                        axios.post("/menu/" + url + ".do", this.formData).then((res) => {
                            if (res.data.flag) {
                                //请求到了数据
                                this.$message({
                                    message: res.data.message,
                                    type: "success"
                                })
                            } else {
                                this.$message.error(res.data.message)
                            }
                            // 关闭dialog
                            this.dialogFormVisible = false
                            // 重新查询数据
                            this.findPage()
                        })
                    })
                },

                // 查询按钮的分页查询
                btFindPage() {
                    // 如果不设置为1,在第二页查询会查不到第一页中的数据
                    this.pagination.currentPage = 1;
                    this.findPage();
                },
                //分页查询菜单
                findPage() {
                    // 构建分页对象参数
                    let QueryPageBean = {
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString: this.pagination.queryString
                    }
                    // 发送请求
                    axios.post("/menu/findMenuPage.do", QueryPageBean).then((res) => {
                        //请求到了数据
                        this.pagination.total = res.data.total
                        // 为页面赋值
                        let rows = res.data.rows;
                        rows.forEach(item=>{
                            let parentOs = {
                                id:item.id,
                                name:item.name
                            }
                            this.parentOptions.push(parentOs)
                            item.hasChildren=true
                        })
                        this.dataList = rows;
                    })
                },
                // 重置表单
                resetForm() {
                    this.formData = {};
                },
                // 弹出添加窗口
                handleCreate() {
                    this.dialogTitle = "新增菜单信息"
                    // 查询角色信息
                    this.resetForm();
                    this.dialogFormVisible = true;
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    // 切换页面时查询查询分页数据
                    this.pagination.currentPage = currentPage
                    this.findPage()
                },
                // 删除--列表中点删除
                handleDelete(row) {
                    this.$confirm('是否删除该菜单信息?', '警告', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.post("/menu/deleteById/" + row.id + ".do").then((res) => {
                            if (res.data.flag) {
                                this.$message({
                                    message: res.data.message,
                                    type: "success"
                                })
                                // 删除完成后重新执行分页
                                this.findPage()
                               // todo 删除或者新增后菜单不会动态加载
                            } else {
                                this.$message({
                                    dangerouslyUseHTMLString: true,
                                    message: res.data.message,
                                    type: "error"
                                })
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '取消删除'
                        });
                    });
                },
                // 弹出编辑窗口--列表中点编辑
                handleUpdate(row) {
                    if (row.level===1){
                        this.$message.error("一级菜单不可编辑!")
                        return
                    }
                    // 先对套餐数据进行赋值
                    this.formData = row
                    this.dialogTitle = "编辑菜单信息"
                    this.dialogFormVisible = true;
                },
            }
        })
    </script>
</html>
